package com.tarena.lbs.basic.web.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.basic.web.mapper.UserGroupMapper;
import com.tarena.lbs.pojo.basic.po.UserGroupPO;
import com.tarena.lbs.pojo.basic.query.UserGroupQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserGroupRepository {
    @Autowired
    private UserGroupMapper userGroupMapper;
    //DAY11-PM  02、后台查询用户人群数据的分页列表
    public PageInfo<UserGroupPO> getUserGroups(UserGroupQuery query, Integer businessId) {
        QueryWrapper<UserGroupPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", businessId);
        PageHelper.startPage(query.getPageNo(),query.getPageSize());// 设置查询条件，根据业务ID进行精确匹配
        return PageInfo.of(userGroupMapper.selectList(queryWrapper));
    }

    /**
     * mapper采用了泛型 ，因此 可以传入泛型参数
     * 下面类似于在mapper.xml文件中写sql语句
     * @param groupName
     * @param businessId
     * @return
     */
    public Long countUserGroupByNameAndBizId(String groupName, Integer businessId) {
        //select count(*) from lbs_user_group where business_id=#{} and group_name=#{};
        // 创建一个查询包装器，用于用户分组的查询条件构建
        QueryWrapper<UserGroupPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id",businessId);
        queryWrapper.eq("group_name",groupName);
        return userGroupMapper.selectCount(queryWrapper);
    }

    public void saveUserGroup(UserGroupPO poParam) {
        userGroupMapper.insert(poParam);
    }
    public List<UserGroupPO> getUserGroupsByTagIds(List<Integer> userTagIds) {

        //FIND_IN_SET(#{元素1},tag_ids) or FIND_IN_SET(#{元素2},tag_ids)
        //mybatis-plus可能不支持FIND_IN_SET函数
        //手写 myabtis的动态sql的映射文件
        return  userGroupMapper.selectUserGroupByTagIds(userTagIds);
    }
}
